
/*
 *
 * All Rights Reserved.
 */
package com.model.demo.designmodel.proxy.dynamicproxy.cglibproxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * CglibMatchmaker
 *
 * @author Liao Dongshan, 2019年06月10日
 *
 */
public class CglibMatchmaker implements MethodInterceptor {

    public Object getInstance (Class<?> clazz){
        //enhancer相当于proxy工具类
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();

    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object invoke = methodProxy.invokeSuper(o, objects);
        after();
        return invoke;
    }

    public void before(){
        System.out.println("【前期工作】：确认需求");
    }

    public void after(){
        System.out.println("【后期工作】：反馈结果");
    }
}
